package com.wh.org.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wh.basic.vo.AjaxResult;
import com.wh.org.dto.EmployeeDto;
import com.wh.org.pojo.Employee;
import com.wh.org.service.IEmployeeService;
import com.wh.system.annotation.PreAuthorize;
import com.wh.user.dao.LoginInfoDao;
import com.wh.user.pojo.LoginInfo;
import com.wh.user.pojo.modify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService service;

    @PreAuthorize(sn = "employee/addOrUpd",name = "管理员添加或修改")
    @PutMapping
    public AjaxResult addOrUpd(@RequestBody Employee employee ){
        try {
            if (employee.getId()!=null){

                service.upd(employee);
                return AjaxResult.me();
            }else{
                service.add(employee);
                return AjaxResult.me();
            }
        }catch (Exception ex){
            return AjaxResult.me().setSuccess(false).setMessage("出现异常");
        }
    }
//    @PreAuthorize(sn = "employee/addOrUpd",name = "管理员添加或修改")
    @PutMapping("/{id}")
    public AjaxResult updState(@PathVariable("id") Long id){
        service.updState(id);
        System.out.println(id);
        return AjaxResult.me();
    }

    @PreAuthorize(sn = "employee/del",name = "通过id删除管理员")
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        service.del(id);
        return AjaxResult.me();
    }

    @PreAuthorize(sn = "employee/findAll",name = "查找全部的管理员")
    @GetMapping
    public List<Employee> findAll(){
        return service.findAll();
    }

    @PreAuthorize(sn = "employee/patchDel",name = "批量删除管理员")
    @PatchMapping
    public AjaxResult patchDel(Long[] ids){
        service.patchDel(ids);
        return AjaxResult.me();
    }

    @PreAuthorize(sn = "employee/findById",name = "通过id查找管理员")
    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id){
        return service.findById(id);
    }

    @PreAuthorize(sn = "employee/findByPage",name = "分页查找管理员列表")
    @PostMapping
    public IPage<Employee> fingByPage(@RequestBody EmployeeDto employeeDto){
        return service.findByPage(employeeDto);
    }


    /**
     * 根据店铺id，查询店铺员工
     * @param shopId
     * @return 店铺员工们
     */

//    @PreAuthorize(sn = "employee/addOrUpd",name = "管理员添加或修改")
//    @GetMapping("list/{shopId}")
    public List<Employee> listByShopId(@PathVariable Long shopId){
        return service.listByShopId(shopId);
    }

    @PutMapping("/modify")
    public AjaxResult modify(@RequestBody modify modify){
        service.modify(modify);
        return AjaxResult.me();
    };
    @PutMapping("/modifyPassword")
    public  AjaxResult modifyPassword(@RequestBody modify modify){
        return service.modifyPassword(modify);
    }

    /**
     * 前端输入完数据后，进行判断输入的手机号或者账户名是否重复
     * @param request
     * @return
     */
    @PostMapping("/checkDuplicate")
    public ResponseEntity<Map<String, Boolean>> checkDuplicate(@RequestBody Map<String, String> request) {
        String type = request.get("type");
        String value = request.get("value");

        if ("account".equals(type)) {
            boolean isDuplicate = service.isAccountExist(value);
            return ResponseEntity.ok(Collections.singletonMap("isDuplicate", isDuplicate));
        } else if ("phone".equals(type)) {
            boolean isDuplicate = service.isPhoneExist(value);
            return ResponseEntity.ok(Collections.singletonMap("isDuplicate", isDuplicate));
        } else {
            // 未知类型，返回错误信息
            return ResponseEntity.badRequest().build();
        }
    }
}

